<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" label-width="68px">
      <el-form-item label="昵称" prop="nickName">
        <el-input
          v-model="queryParams.nickName"
          placeholder="请输入昵称"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="用户姓名" prop="realName">
        <el-input
          v-model="queryParams.realName"
          placeholder="请输入用户姓名"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="电话号码" prop="phone">
        <el-input
          v-model="queryParams.phone"
          placeholder="请输入电话号码"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="审核状态" prop="status">
        <el-select v-model="queryParams.status" placeholder="请选择审核状态" clearable size="small">
          <el-option
            v-for="dict in statusOptions"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="认证类型" prop="status">
        <el-select v-model="queryParams.type" placeholder="请选择认证类型" clearable size="small">
          <el-option label="身份证" value="1"/>
          <el-option label="工作证" value="2"/>
          <el-option label="学历证" value="3"/>
          <el-option label="离婚证" value="4"/>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">

      <el-col :span="1.5">
        <el-button
          type="warning"
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['wx:auth:export']"
        >导出</el-button>
      </el-col>
<!--      <el-col :span="1.5">-->
<!--        <el-button-->
<!--          type="primary"-->
<!--          icon="el-icon-plus"-->
<!--          size="mini"-->
<!--          @click="checkPass"-->
<!--          v-hasPermi="['wx:auth:update']"-->
<!--        >通过</el-button>-->
<!--      </el-col>-->
<!--      <el-col :span="1.5">-->
<!--        <el-button-->
<!--          type="danger"-->
<!--          icon="el-icon-close"-->
<!--          size="mini"-->
<!--          @click="checkClose"-->
<!--          v-hasPermi="['wx:auth:update']"-->
<!--        >禁用</el-button>-->
<!--      </el-col>-->
    </el-row>

    <el-table v-loading="loading" :data="authList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="微信Id" align="center" prop="wxUserId" />
      <el-table-column label="昵称" align="center" prop="nickName" :show-overflow-tooltip="true"/>
      <el-table-column label="微信头像" align="center" prop="headUrl">
        <template slot-scope="scope">
          <el-image :src="scope.row.headUrl" style="height: 40px;width: 40px"/>
        </template>
      </el-table-column>
      <el-table-column label="手机号" align="center"    prop="phone" />
      <el-table-column label="真实姓名" align="center"  prop="realName" />
      <el-table-column label="性别" align="center" :formatter="genderFormat"/>
      <el-table-column label="是否人脸核验" align="center" prop="isFacePass" >
        <template slot-scope="scope">
          <span v-if="scope.row.isFacePass == 0" class="status_0">未通过</span>
          <span v-if="scope.row.isFacePass == 1" class="status_1">已通过</span>
        </template>
      </el-table-column>
      <el-table-column label="认证类型" align="center" prop="type" >
        <template slot-scope="scope">
          <span v-if="scope.row.type == 1" class="status_0">身份证</span>
          <span v-if="scope.row.type == 2" class="status_1">工作证</span>
          <span v-if="scope.row.type == 3" class="status_1">学历证</span>
          <span v-if="scope.row.type == 4" class="status_1">离婚证</span>
        </template>
      </el-table-column>
      <el-table-column  label="审核状态" align="center" prop="status" >
        <template slot-scope="scope">
          <span  :class="'status_'+scope.row.status">{{ statusFormat(scope.row.status) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleCheck(scope.row)"
            v-hasPermi="['wx:auth:edit']"
            v-if="scope.row.status == 0"
          >审核</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-s-fold"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['wx:auth:edit']"
          >详情</el-button>

<!--          <el-button-->
<!--            size="mini"-->
<!--            type="text"-->
<!--            icon="el-icon-delete"-->
<!--            @click="handleDelete(scope.row)"-->
<!--            v-hasPermi="['wx:auth:remove']"-->
<!--          >删除</el-button>-->
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改个人认证对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-descriptions border :column="1">
        <el-descriptions-item label="认证照片"  v-for="(item,index ) in picArray" :key="index">
          <el-image :src="item"  :preview-src-list="picArray"/>
        </el-descriptions-item>
      </el-descriptions>
      <div slot="footer" class="dialog-footer-1" v-if="isChecking">
        <el-button type="primary" @click="check('pass')">通过</el-button>
        <el-button type="warning" @click="check('close')">拒绝</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {  listAuth, getAuth, delAuth, addAuth, updateAuth, exportAuth } from "@/api/wx/auth";
import {resetUserPwd} from "@/api/system/user";

export default {
  name: "Auth",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 总条数
      total: 0,
      // 个人认证表格数据
      authList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        id:undefined,
        wxUserId:undefined,
        pic: undefined,
        isFacePass: undefined,
        status :undefined,
        type:undefined,
        outReason:undefined
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      },
      statusOptions:[],
      genderStatus:[],
      isChecking:false,
      picArray:[]
    };
  },
  created() {
    this.getList();
    this.getDicts("comment_status").then(response => {
      this.statusOptions = response.data;
    });
    this.getDicts("wx_user_sex").then(response => {
      this.genderStatus = response.data;
    });
  },
  // watch: {
  //   $route(to, from) {
  //     //当参数改变时就会触发比方法，以此达到数据属性
  //
  //   }
  // },
  methods: {
    /** 查询个人认证列表 */
    getList() {
      this.loading = true;
      listAuth(this.queryParams).then(response => {
        this.authList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 审核状态(0待审核1通过2不通过)字典翻译
    statusFormat(status, column) {
      return this.selectDictLabel(this.statusOptions, status);
    },
    // 性别字典翻译
    genderFormat(row) {
      return this.selectDictLabel(this.genderStatus, row.gender);
    },
    // 表单重置
    reset() {
      this.form = {
        id:undefined,
        wxUserId: undefined,
        pic: undefined,
        status :undefined,
        type:undefined,
        outReason:undefined
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!=1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加个人认证";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.isChecking = false;
      this.reset();
      const id = row.id || this.ids
      getAuth(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "个人认证详情";
        if(this.form.pic){
          this.picArray = this.form.pic.split(',')
          // console.log(this.picArray)
        }
      });
    },
    handleCheck(row){
      this.isChecking = true;
      this.reset();
      const id = row.id
      this.id = id;
      getAuth(id).then(response => {
        this.form = response.data;
        this.open = true;
        if(this.form.pic){
            this.picArray = this.form.pic.split(',')
        }
        this.title = "个人认证审核";
      });
    },
    check(status){
      if(status === 'pass'){
        this.form.status = '1'
        updateAuth({id:this.form.id,status:this.form.status,outReason:this.form.outReason}).then(res=>{
          if(res.code == 200){}
          this.msgSuccess("修改成功");
          this.open = false;
          this.reset();
          this.getList();
        })
      }else{
        this.$prompt('请输入拒绝原因', "提示", {
          confirmButtonText: "确定",
          cancelButtonText: "取消"
        }).then(({ value }) => {
          this.form.outReason = value
          this.form.status = '2'
          updateAuth({id:this.form.id,status:this.form.status,outReason:this.form.outReason}).then(res=>{
            if(res.code == 200){}
            this.msgSuccess("修改成功");
            this.open = false;
            this.reset();
            this.getList();
          })
        }).catch(() => {});

      }


    },
    /** 提交按钮 */
    submitForm: function() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.wxUserId != undefined) {
            updateAuth(this.form).then(response => {
              if (response.code === 200) {
                this.msgSuccess("修改成功");
                this.open = false;
                this.getList();
              }
            });
          } else {
            addAuth(this.form).then(response => {
              if (response.code === 200) {
                this.msgSuccess("新增成功");
                this.open = false;
                this.getList();
              }
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const wxUserIds = row.wxUserId || this.ids;
      this.$confirm('是否确认删除个人认证编号为"' + wxUserIds + '"的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return delAuth(wxUserIds);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        }).catch(function() {});
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm('是否确认导出所有个人认证数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return exportAuth(queryParams);
        }).then(response => {
          this.download(response.msg);
        }).catch(function() {});
    }
  }
};
</script>
<style scoped>
 .status_0{
   color: gray!important;
 }
 .status_1{
   color: green;
 }
 .status_2{
   color: red;
 }
 .dialog-footer-1{
   box-sizing: border-box;
   padding: 20px;
   justify-content: space-around;
   display: flex;
 }
</style>
